package com.huichaoliu.data;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

public class UserInfo {

	private SharedPreferences userInfo = null; 
	private Editor editor = null;
	private boolean isFirstLogin = true;
	private String uid = null;
	private String mobile = null;
	private String nickname = null;
	private String area;
	private String avatar;
	private String occupation;
	private String signature;
	private String sex;
	private int type;
	private String constellation;
	private int collocationNumber;
	private int age;
	private String hobby;
	private String user;
	
	public UserInfo(Context mContext){
		userInfo = mContext.getSharedPreferences("user_info", 0);
		editor = userInfo.edit();
		
		this.isFirstLogin = userInfo.getBoolean("isFirstLogin", true);
		this.uid = userInfo.getString("uid", null);
		this.mobile = userInfo.getString("mobile", null);
		this.nickname = userInfo.getString("nickname", null);
		this.area = userInfo.getString("area", null);
		this.avatar = userInfo.getString("avatar", null);
		this.occupation = userInfo.getString("occupation", null);
		this.signature = userInfo.getString("signature", null);
		this.sex = userInfo.getString("sex", null);
		this.type = userInfo.getInt("type", 0);
		this.constellation = userInfo.getString("constellation", null);
		this.user = userInfo.getString("user", null);
		this.age = userInfo.getInt("age", 0);
		this.hobby = userInfo.getString("hobby", null);
	}
	
	public void commit(){
		editor.commit();
	}
	
	public void clear(){
		editor.clear();
		editor.commit();
	}

	public boolean isFirstLogin() {
		return isFirstLogin;
	}

	public void setFirstLogin(boolean isFirstLogin) {
		this.isFirstLogin = isFirstLogin;
		editor.putBoolean("isFirstLogin", isFirstLogin);
	}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
		editor.putString("uid", uid);
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
		editor.putString("mobile", mobile);
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
		editor.putString("nickname", nickname);
	}

	public String getArea() {
		return area;
	}

	public void setArea(String area) {
		this.area = area;
		editor.putString("area", area);
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
		editor.putString("avatar", avatar);
	}

	public String getOccupation() {
		return occupation;
	}

	public void setOccupation(String occupation) {
		this.occupation = occupation;
		editor.putString("occupation", occupation);
	}

	public String getSignature() {
		return signature;
	}

	public void setSignature(String signature) {
		this.signature = signature;
		editor.putString("signature", signature);
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
		editor.putString("sex", sex);
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
		editor.putInt("type", type);
	}

	public String getConstellation() {
		return constellation;
	}

	public void setConstellation(String constellation) {
		this.constellation = constellation;
		editor.putString("constellation", constellation);
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
		editor.putInt("age", age);
	}

	public String getHobby() {
		return hobby;
	}

	public void setHobby(String hobby) {
		this.hobby = hobby;
		editor.putString("hobby", hobby);
	}
	
	public int getCollocationNumber() {
		return collocationNumber;
	}

	public void setCollocationNumber(int collocationNumber) {
		this.collocationNumber = collocationNumber;
		editor.putInt("collocationNumber", collocationNumber);
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
		editor.putString("user", user);
	}

	public static void initData(JSONObject jo,Context mContext) throws JSONException{
		UserInfo info = new UserInfo(mContext);
		info.setArea(jo.getString("area"));
		info.setAvatar(jo.getString("image"));
		info.setOccupation(jo.getString("occupation"));
		info.setSignature(jo.getString("signature"));
		info.setSex(jo.getString("sex"));
		info.setType(jo.getInt("type"));
		info.setUid(jo.getString("userId"));
		info.setConstellation(jo.getString("constellation"));
		info.setMobile(jo.getString("phone"));
		info.setNickname(jo.getString("nickname"));
		info.setCollocationNumber(jo.getInt("collocationNumber"));
		info.setUser(jo.getString("user"));
		info.setAge(jo.getInt("age"));
		info.setHobby(jo.getString("hobby"));
		info.commit();
	}
	
}
